package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.supervision.CheckItem;
import com.example.entity.supervision.CheckResult;
import com.example.mapp.CheckResultMapp;
import com.example.service.CheckResultService;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2018/8/9.
 */
@Service
public class CheckResultServiceImpl implements CheckResultService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(PlanDetailServiceImpl.class);

    @Autowired
    CheckResultMapp checkResultMapp;

    @Override
    public Map<String, Object> queryByScrid(Map map) {
        try{
            List<Map> list = checkResultMapp.query(map);
            List<Map<String, Object>> dolist = new ArrayList();
            for ( Map cr:list ) {
                if(dolist.size()==0){
                    Map<String,Object> map2 = new HashMap();
                    Map<String,Object> map3 = new HashMap();
                    map3.put("itemContent",cr.get("itemContent"));
                    map3.put("id",cr.get("id"));
                    map3.put("result",cr.get("result"));
                    List<Map> list1 = new ArrayList();
                    list1.add(map3);
                    map2.put("itemName",cr.get("itemName"));
                    map2.put("itemContents",list1);
                    dolist.add(map2);
                }else{
                    boolean isadd = false;
                    for (Map m: dolist ) {
                        if((cr.get("itemName")+"").equals(m.get("itemName")+"")){
                            Map<String,Object> map4 = new HashMap();
                            map4.put("itemContent",cr.get("itemContent"));
                            map4.put("id",cr.get("id"));
                            map4.put("result",cr.get("result"));
                            ((List)m.get("itemContents")).add(map4);
                            isadd = true;
                            break;
                        }
                    }
                    if(!isadd){
                        Map<String,Object> map5 = new HashMap();
                        Map<String,Object> map3 = new HashMap();
                        List<Map> list1 = new ArrayList();
                        map3.put("itemContent",cr.get("itemContent"));
                        map3.put("id",cr.get("id"));
                        map3.put("result",cr.get("result"));
                        list1.add(map3);
                        map5.put("itemName",cr.get("itemName"));
                        map5.put("itemContents",list1);
                        dolist.add(map5);
                    }

                }
            }
            Map res = new HashMap();
            res.put("list",dolist);
            return ResultUtil.returnSuccess("查询成功", res);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败", null);
        }
    }

    @Override
    public Map<String, Object> add(CheckResult map) {
        try{
            checkResultMapp.insert(map);
            return ResultUtil.returnSuccess("新增成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("新增失败", null);
        }
    }
}
